$(function() {
	//列表
	easyExt.initTreeGrid('#tg','/sale/list'); 
	easyExt.initDataGrid('#dg','/sale/getSaleItem');
	
	//添加订单实现
	$("#add").click(function(){
		easyExt.save('#addOrderForm','#addOrderDialog','/sale/add',function(){
			$('#tg').treegrid('reload'); 
		});
	});
	//修改订单实现
	$("#edit").click(function(){
		var selRows=$('#tg').treegrid('getSelections');
		easyExt.update('#addOrderForm','#addOrderDialog',selRows,'/sale/edit',function(){
			$('#tg').treegrid('clearSelections');
			$('#tg').treegrid('reload'); 
		});
	});	
	//删除订单明细实现
	$("#delItem").click(function(){
		var selRows=$('#item_dg').datagrid('getSelections');
		easyExt.del(selRows,'/sale/deleteItem',function(){
			$('#item_dg').datagrid('clearSelections');
			$('#item_dg').datagrid('reload'); 
		});
	});
	//添加订单明细实现
	$("#addItem").click(function(){
		var selRows=$('#tg').datagrid('getSelections');
		easyExt.add('/sale/addItem/'+selRows[0].id,function(){
			$('#item_dg').datagrid('reload'); 
		});
	});
	//修改订单明细实现
	$("#editItem").click(function(){
		var selRows=$('#item_dg').datagrid('getSelections');
		easyExt.edit(selRows,'/sale/editItem',function(){
			$('#item_dg').datagrid('clearSelections');
			$('#item_dg').datagrid('reload'); 
		});
	});
	//查看订单明细实现
	$("#viewItem").click(function(){
		var selRows=$('#tg').datagrid('getSelections');
		if(selRows.length==0){
			$.messager.alert("提示", "请选择要查看的订单！", "info");  
			return;
		}else if(selRows.length==1){
			getSaleItem(selRows[0]);
		}else{
			$.messager.alert("提示", "只能选择一行！", "info");
		}
	});
	function getSaleItem(sale){
		$('#saleItem').dialog({
			title:'管理订单明细'
		});
		$('#saleItem').dialog('open');
		easyExt.initDataGrid('#item_dg','/sale/getSaleItem?saleId='+sale.id);
		$('#item_dg').datagrid('clearSelections'); 
	}	
	//产品下拉框
	$("#product").combobox({
		url: easyExt.url+'/sale/productTree',
		valueField:'id',
		textField:'productName'
	});
	//合作店下拉框
	$("#partner").combobox({
		url: easyExt.url+'/sale/partnerTree',
		valueField:'id',
		textField:'partnerName'
	});
	//下单
	$("#custom").click(function(){
		var selRows=$('#dg').datagrid('getSelections');
		if(selRows.length==0){
			$.messager.alert("提示", "请选择要操作的行！", "info");  
			return;
		}else{
			var ids="";  
	        //批量获取选中行的ID  
	        for (i = 0; i < selRows.length;i++) {  
	            if (ids =="") {  
	            	ids = selRows[i].id;  
	            } else {  
	            	ids = selRows[i].id + "," + ids;  
	            }                 
	        }
	        $.messager.confirm('提示', '是否对选中产品下单?', function (r) { 
	        	 if (r) {  
	        		 $.ajax({
	        			 url: easyExt.url+'/sale/customItems?ids='+ids,
	        			 success:function(data){
	        				 if(data.success){
	        					 $.messager.show({
	        							title: '提示',
	        							msg: data.msg,
	        							timeout:2000
	        						});
	        					 $('#dg').datagrid('reload'); 
	        				 }else{
	        					 $.messager.alert("提示", data.msg, "error"); 
	        				 } 
	        			 }	 
	        		 });
	             }  
	        });
		}
	});
});
